package com.controller;

import com.dto.NoticeDTO;
import com.dto.ReaderCommentDTO;
import com.service.LoginServiceImpl;
import com.service.ReaderCommentServiceImpl;
import com.util.LykFileUtil;
import com.util.Result;
import com.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * @author linyukun
 * @date 2022/10/10 16:41
 */
@RequestMapping("readerComment")
@RestController
public class ReaderCommentController {
    @Autowired
    private ReaderCommentServiceImpl readerCommentServiceImpl;

    @Autowired
    private LoginServiceImpl loginServiceImpl;

    @Value("${image.upload.path.readerComment}")
    private String imageUploadPathReaderComment;

    /**
     * 新增读者留言信息
     * @author linyukun
     * @date 2022-10-10 16:44:00
     */
    @RequestMapping(value="/addReaderComment",method = RequestMethod.POST)
    public Result addReaderComment(@RequestBody ReaderCommentDTO dto){
        try{
            String userName = loginServiceImpl.isLogin();
            dto.setCreateUser(userName);
            return ResultUtil.returnDataSuccess(readerCommentServiceImpl.addReaderComment(dto));
        }catch(Exception e){
            e.printStackTrace();
            return ResultUtil.returnFailMsg(e.getMessage());
        }
    }

    /**
     * 删除读者留言信息
     * @author linyukun
     * @date 2022-10-10 16:44:00
     */
    @RequestMapping(value="/delReaderComment",method = RequestMethod.POST)
    public Result delReaderComment(@RequestBody ReaderCommentDTO dto){
        try{
            String userName = loginServiceImpl.isLogin();
            dto.setDeleteUser(userName);
            return ResultUtil.returnDataSuccess(readerCommentServiceImpl.delReaderComment(dto));
        }catch(Exception e){
            e.printStackTrace();
            return ResultUtil.returnFailMsg(e.getMessage());
        }
    }


    /**
     * 回复读者留言信息
     * @author linyukun
     * @date 2022-10-10 16:44:00
     */
    @RequestMapping(value="/replyReaderReply",method = RequestMethod.POST)
    public Result replyReaderReply(@RequestBody ReaderCommentDTO dto){
        try{
            String userName = loginServiceImpl.isLogin();
            dto.setReplyUser(userName);
            return ResultUtil.returnDataSuccess(readerCommentServiceImpl.replyReaderReply(dto));
        }catch(Exception e){
            e.printStackTrace();
            return ResultUtil.returnFailMsg(e.getMessage());
        }
    }

    /**
     * 获取读者留言列表
     * @author linyukun
     * @date 2022-10-10 16:46:00
     */
    @RequestMapping(value="/getReaderCommentList",method = RequestMethod.POST)
    public Result getReaderCommentList(@RequestBody ReaderCommentDTO dto){
        try{
            String userName = loginServiceImpl.isLogin();
            if(!"admin".equals(dto.getIdentity())){
                dto.setCreateUser(userName);
            }
            return ResultUtil.returnDataSuccess(readerCommentServiceImpl.getReaderCommentList(dto));
        }catch(Exception e){
            e.printStackTrace();
            return ResultUtil.returnFailMsg(e.getMessage());
        }
    }


    //保存活动海报
    @RequestMapping("/saveReaderCommentPic")
    public String saveReaderCommentPic(MultipartFile file) throws IOException {
        MultipartFile[] files = new MultipartFile[]{file};
        //保存图片文件到本地并返回相对路径
        //List<String> image = LykFileUtil.uploads("/planCat_file/", files, "image");
        String currentPath = System.getProperty("user.dir");
        List<String> image = LykFileUtil.uploads(imageUploadPathReaderComment, files, "image");
        //拿着图片路径存储到数据库或者做其他处理,这里就先不多写了，自行发挥
        if(!CollectionUtils.isEmpty(image)){
            //return image.get(0).replace("/view/src/main/resources/static/","");
            return currentPath + image.get(0);
        }else{
            return "";
        }
    }


}
